﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Senac.Escola.Projeto.Models;
using Senac.Escola.Projeto.Models.Context;

namespace Senac.ProjetoIntegrador.EducacaoWeb.Controllers
{ 
    public class AlunoController : Controller
    {
        private Context db = new Context();

        public ViewResult Index()
        {
            var aluno = db.Aluno.Include(a => a.Usuario).Include(a => a.Categoria);
            return View(aluno.ToList());
        }


        public ViewResult Details(int id)
        {
            Aluno aluno = db.Aluno.Find(id);
            return View(aluno);
        }

        public ActionResult Create()
        {
            ViewBag.UsuarioID = new SelectList(db.Usuario, "UsuarioID", "Login");

            ViewBag.CategoriaID = new SelectList(db.Categoria, "CategoriaID","Nome");

            return View();
        } 


        [HttpPost]
        public ActionResult Create(Aluno aluno)
        {
            if (ModelState.IsValid)
            {
                //verificar se já não existe esse usuario
                var query1 = db.Usuario.Where(p => p.Login == aluno.Usuario.Login);
                Usuario _Usuario = new Usuario();
                foreach (var item1 in query1)
                {
                    _Usuario = item1;
                }
                db.SaveChanges();

                if (_Usuario.Login == null)
                {
                    //identificar o tipo
                    var query = db.TipoUsuario.Where(p => p.Nome == "ALUNO");
                    TipoUsuario _TipoUsuario = new TipoUsuario();
                    foreach (var item in query)
                    {
                        _TipoUsuario = item;
                    }
                    db.SaveChanges();

                    //adicionar Aluno e Usuário e vincular categoria
                    aluno.Usuario.TipoUsuarioID = _TipoUsuario.TipoUsuarioID;
                    db.Aluno.Add(aluno);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    //mandar msg dizendo que esse login já existe
                }
            }

            ViewBag.UsuarioID = new SelectList(db.Usuario, "UsuarioID", "Login", aluno.UsuarioID);
            ViewBag.CategoriaID = new SelectList(db.Categoria, "CategoriaID", "Nome", aluno.CategoriaID);
            return View(aluno);
        }
      
        public ActionResult Edit(int id)
        {
            Aluno aluno = db.Aluno.Find(id);
            ViewBag.UsuarioID = new SelectList(db.Usuario, "UsuarioID", "Login", aluno.UsuarioID);
            ViewBag.CategoriaID = new SelectList(db.Categoria, "CategoriaID", "Nome", aluno.CategoriaID);
            return View(aluno);
        }

        [HttpPost]
        public ActionResult Edit(Aluno aluno)
        {
            if (ModelState.IsValid)
            {
                db.Entry(aluno).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UsuarioID = new SelectList(db.Usuario, "UsuarioID", "Login", aluno.UsuarioID);
            ViewBag.CategoriaID = new SelectList(db.Categoria, "CategoriaID", "Nome", aluno.CategoriaID);
            return View(aluno);
        }
 
        public ActionResult Delete(int id)
        {
            Aluno aluno = db.Aluno.Find(id);
            return View(aluno);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Aluno aluno = db.Aluno.Find(id);
            db.Aluno.Remove(aluno);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}